<?php
//THE TEMPLATE FOR OUR OUTPUT
 
// No direct access
defined('_JEXEC') or die('Restricted access'); 

/* Gets the difference between day 1 and day 2. 
 * 
 * Dates must be strings specified in format YYYY-MM-DD
 * 
 * If 	earlier_date == 2011-01-01 and
 * 		later_date == 2011-01-09, 
 * 
 * the Duration will be 9 days. 01 = 1 | 02 = 2 | ... | 09 = 9
 * 
 * If the earlier_date is greater than the later_date, this function returns zero. 
 */
function getDuration($earlier_date, $later_date)
{
	$later_year = substr($later_date, 0, 4);
	$later_month = substr($later_date, 5, 2);
	$later_day = substr($later_date, 8, 2);
	
	$earlier_year = substr($earlier_date, 0, 4);
	$earlier_month = substr($earlier_date, 5, 2);
	$earlier_day = substr($earlier_date, 8, 2);
	
	$later_culm = ($later_year * 365) + ($later_month * 12) + ($later_day);
	$earlier_culm = ($earlier_year * 365) + ($earlier_month * 12) + ($earlier_day);
	
	if($earlier_culm > $later_culm)
		return 0;
	else 
		return $later_culm - $earlier_culm + 1;
}


?>

<h2> Summer Camp Listings</h2>
 <p><strong><a href="index.php?option=com_summercamps&controller=campform">Submit a summer camp</a></strong></p>

<form class="camps" action="index.php" method="post" name="adminSearch" id="adminSearch">
	<input class="fulltext large" type="text" name="Search" id="Search" 
				size="32" maxlength="256" value="" />
	<input type="submit" class="submitbtn" value="Search">
	<input type="hidden" name="option" value="com_summercamps" />
	<input type="hidden" name="issearch" value="true"/>
	<input type="hidden" name="task" value="" />
	<!-- <input type="hidden" name="controller" value="camp" />  DEFAULT -->
</form>
<br>
<table class="campslist">
<thead>
    <tr>
        <th><?php echo JText::_( 'CampName' ); ?></th>
        <th><?php echo JText::_( 'Region' ); ?></th>
        <th><?php echo JText::_( 'City' ); ?></th>
        <th><?php echo JText::_( 'StartDate' ); ?></th>
        <th><?php echo JText::_( 'Duration' ); ?></th>
        <th><?php echo JText::_( 'Ages' ); ?></th>
    </tr>
</thead>
<?php
$k = 0;
    
/*We are able to access $this->items[$i] because in this view's
* view.html.php, we had the lines:
* 		// Gets the data(reference) from the model
* 		$items =& $this->get( 'Data');
* 		//Sticks the data(reference) into an items variable that is passed to the template (this file)
* 		$this->assignRef( 'items', $items ); 
*/
for ($i=0, $n=count( $this->items ); $i < $n; $i++)
{
	$row =& $this->items[$i];
	$checked    = JHTML::_( 'grid.id', $i, $row->ID );
	$link = JRoute::_( 'index.php?option=com_summercamps&controller=single&cid='. $row->ID );
	?>
        
<tr>
<td><a href="<?php echo $link; ?>"><?php echo $row->CampName; ?></a></td>
<td><?php echo $row->Region; ?>			</td>
<td><?php echo $row->City; ?>			</td>
<td><?php echo $row->StartDate; ?>		</td>
<td><?php 


$duration = getDuration($row->StartDate, $row->EndDate);

echo $duration; ?>		</td>
<td><?php 
	/*Let's print the age range if one exists */
	if($row->MinAge == "99" && $row->MaxAge == "99")
		echo "unspecified";
	elseif($row->MinAge && $row->MaxAge)
		echo $row->MinAge." - ".$row->MaxAge;
	elseif($row->MinAge)//only a min-age exists
		echo $row->MinAge." and older";
	elseif($row->MaxAge)//only a max-age exists
		echo $row->MaxAge." and younger";
	else				//unspecified
		echo "all ages";
		
?>		

</td>

</tr>
<?php
$k = 1 - $k;//k alternates between 1 and 0 to give odd/even rows
}
?>
</table>
